package com.zhaoxi.demo.controllers;

import com.zhaoxi.demo.commons.VirtualData;
import com.zhaoxi.demo.models.JsonResult;
import com.zhaoxi.demo.models.Student;
import com.zhaoxi.demo.models.StudentQueryFilter;
import com.zhaoxi.demo.models.StudentViewModel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Infor on 2017/07/02.
 * <p>
 * Form 表单提交实例
 */

@Controller
@RequestMapping("/student")
public class StudentController {

    @RequestMapping("/list")
    public String list(Model model, StudentQueryFilter filter) {

        if (filter.getPageSize() <= 0) {
            filter.setPageIndex(1);
        }
        if (filter.getPageSize() <= 0) {
            filter.setPageSize(10);
        }

        List<Student> students = VirtualData.query(filter);
        model.addAttribute("students", students);
        model.addAttribute("filter", filter);
        return "/student/list";
    }

    @RequestMapping("/add")
    public String add(Model model) {
        model.addAttribute("viewModel", new StudentViewModel());
        return "/student/add";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable int id, Model model) {
        Student student = VirtualData.query(id);
        StudentViewModel viewModel = new StudentViewModel(student);
        model.addAttribute("viewModel",viewModel);
        return "/student/edit";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@Valid @ModelAttribute("viewModel") StudentViewModel viewModel, BindingResult validResult,
                             Model model, RedirectAttributes redirectAttributes) {
        if (validResult.hasErrors()) {
            return "/student/add";
        }
        Student student = new Student();
        student.setAge(viewModel.getAge());
        student.setEmail(viewModel.getEmail());
        student.setName(viewModel.getName());
        student.setPhone(viewModel.getPhone());
        VirtualData.add(student);
        return "redirect:/student/list";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String edit(@Valid @ModelAttribute("viewModel") StudentViewModel viewModel, BindingResult validResult,
                             Model model, RedirectAttributes redirectAttributes) {
        if (validResult.hasErrors()) {
            return "/student/edit";
        }
        Student student = new Student();
        student.setId(viewModel.getId());
        student.setAge(viewModel.getAge());
        student.setEmail(viewModel.getEmail());
        student.setName(viewModel.getName());
        student.setPhone(viewModel.getPhone());
        VirtualData.edit(student);
        return "redirect:/student/list";
    }


    @RequestMapping(value = "/api/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Student> addApiStudent(Student student) {
        VirtualData.add(student);
        return new JsonResult<Student>(true, student);
    }

    @RequestMapping(value = "/api/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<String> delete(@PathVariable int id) {
        VirtualData.delete(id);
        return new JsonResult<String>(true);
    }
}
